package com.cq.smartelebicycle.view;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import com.cq.smartelebicycle.R;
import com.cq.smartelebicycle.fragment.BluetoothFragment;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BluetoothListAdapter extends BaseAdapter{
	
	private BluetoothFragment fragment;
	private Context context;
	private List<BluetoothDevice> list;
    public final class ListItemView{ 

        public TextView btname;    
        public ImageView pairBt;    
        public ImageView pairIcon;              
 } 
	
	public BluetoothListAdapter(BluetoothFragment fragment,List<BluetoothDevice> list){
		this.context = fragment.getActivity();
		this.fragment = fragment;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int index, View convertView , ViewGroup parent) {
		// TODO Auto-generated method stub
		ListItemView  listItemView = null;  
		final BluetoothDevice device =list.get(index);
		if(convertView==null){
			listItemView = new ListItemView();
			convertView = LayoutInflater.from(context).inflate(R.layout.bluetooth_item, null);
			listItemView.btname = (TextView)convertView.findViewById(R.id.bluetooth_name);
			listItemView.pairBt =(ImageView)convertView.findViewById(R.id.bluetooth_operator_bt);
			listItemView.pairIcon=(ImageView)convertView.findViewById(R.id.bluetooth_state_icon);
			convertView.setTag(listItemView);
			
		}else{
			listItemView = (ListItemView) convertView.getTag();
			
		}
		
		String name = device.getName();
		Log.d("SIMMON","name="+name);
		if(name==null){
			name = device.getAddress();
		}
		Log.d("SIMMON","name="+name);
		listItemView.btname.setText(name);
		
		if(device.getBondState()==BluetoothDevice.BOND_NONE){
			listItemView.pairBt.setImageResource(R.drawable.pair_bt);
			listItemView.pairIcon.setImageResource(R.drawable.pair_icon);
		}else if(list.get(index).getBondState()==BluetoothDevice.BOND_BONDED){
			listItemView.pairBt.setImageResource(R.drawable.unpair_bt);
			listItemView.pairIcon.setImageResource(R.drawable.unpair_icon);
		}
		

		listItemView.pairBt.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
		          Method createBondMethod;
				 if (device.getBondState() == BluetoothDevice.BOND_NONE) {  
		
					try {
						createBondMethod = BluetoothDevice.class  
						            .getMethod("createBond"); 
	                    try {
							createBondMethod.invoke(device);
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					} catch (NoSuchMethodException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}  

				 }else if(device.getBondState() == BluetoothDevice.BOND_BONDED){ 
					 fragment.cancelconnect(device);  
					 try {
						createBondMethod = BluetoothDevice.class  
					            .getMethod("removeBond");
						try {
							createBondMethod.invoke(device);
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					 } catch (NoSuchMethodException e) {
						 
					 }
					
					 
	             }  
			}
			
		});
		
		return convertView;
	}
	

	
}
